package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmBusiness;
import com.nuoniu.sibanyun.entity.dto.CrmBusinessDto;
import com.nuoniu.sibanyun.entity.dto.SaleFunnelDto;
import com.nuoniu.sibanyun.entity.vo.CrmBusinessVo;
import com.nuoniu.sibanyun.service.crm.ICrmBusinessService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户商机 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/crm/business")
@Api(value="客户商机 ",tags={"商机"})
public class CrmBusinessController extends BaseController {

    @Autowired
    private ICrmBusinessService businessService;

//    /**
//     * 获取商机列表
//     * @author 小啊刚
//     * @date 2020/7/8
//     */
//    @GetMapping("page")
//    public HttpResult pageBusiness(Page page, CrmBusiness business) {
//        return HttpResult.ok(businessService.pageBusiness(page, business));
//    }

    /**
     * 保存客户商机
     * @author 小啊刚
     * @date 2020/7/8
     */
    @PostMapping("save")
    @ApiOperation(value = "保存",tags = "APP_商机")
    public HttpResult saveBusiness(@RequestBody CrmBusinessDto businessDto) {
        if(StringUtils.isBlank(businessDto.getTitle())){
            throw new NuoNiuException("商机标题不为空");
        }
        UserInfo userInfo = UserThreadLocal.get();
        LocalDateTime now = LocalDateTime.now();
        CrmBusiness business = new CrmBusiness();
        BeanUtils.copyProperties(businessDto,business);
        //获取商机编码
        String businessNo = businessService.getBusinessNo(userInfo.getCompanyId());
        if(business.getId() == null) {
            business.setCompanyId(userInfo.getCompanyId());
            business.setCreateUserId(userInfo.getUserId());
            business.setCreateUserName(userInfo.getRealName());
            business.setCreateDate(now);
            business.setProjectNo(businessNo);
        }else{
            business.setProjectNo(null);
        }
        business.setUpdateDate(now);
        business.setUpdateUserId(userInfo.getUserId());
        business.setUpdateUserName(userInfo.getRealName());

        businessService.saveOrUpdate(business);
        return HttpResult.ok();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "详情",tags = "APP_商机")
    public HttpResult<CrmBusinessVo> detail(Integer id) {
        if(null == id || id < 1){
            throw new NuoNiuException("参数错误");
        }
        return HttpResult.ok(businessService.detailById(id));
    }

    /**
     * 停用商机
     * @author 小啊刚
     * @date 2020/7/8
     */
    @PostMapping("disable")
    @ApiOperation(value = "停用",tags = "APP_商机")
    public HttpResult update(@RequestBody List<Long> ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("status",2);
        updateWrapper.in("id",ids);
        businessService.update(updateWrapper);
        return HttpResult.ok();
    }

    /**
     * 获取 没有关联 活动非 商机
     * @return
     */
    @GetMapping("listNoDistributeCrmBusiness")
    @ApiOperation(value = "获取 没有关联 活动非 商机",tags = "APP_商机")
    public HttpResult<List<CrmBusiness>> listNoDistributeCrmBusiness() {
        return HttpResult.ok(businessService.listNoDistributeCrmBusiness());
    }

    /**
     * 获取市场活动关联的商机
     * @param activityId
     * @return
     */
    @GetMapping("listActivityBusiness")
    @ApiOperation(value = "获取市场活动关联的商机",tags = "APP_商机")
    public HttpResult<List<CrmBusiness>> listActivityBusiness(Integer activityId) {
        return HttpResult.ok(businessService.listActivityBusiness(activityId));
    }

    /**
     * 统计销售漏斗
     * @param saleFunnelDto
     * @return
     */
    @PostMapping("/saleFunnelStatistic")
    public HttpResult  saleFunnelStatistic(@RequestBody SaleFunnelDto saleFunnelDto){
        return HttpResult.ok(businessService.saleFunnelStatistic(saleFunnelDto));
    }
}
